package com.cj.wtrails.http;

import android.content.Context;

import com.cj.wtrails.utils.NetUtils;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * get缓存方式拦截器
 * Created by WZG on 2016/10/26.
 */

public class CacheInterceptor implements Interceptor {
	
	private Context mContext;

	public CacheInterceptor(Context context) {
		mContext = context;
	}

	@Override
	public Response intercept(Chain chain) throws IOException {
		Request request = chain.request();
		//没网强制从缓存读取(必须得写，不然断网状态下，退出应用，或者等待一分钟后，就获取不到缓存）
		if (!NetUtils.isNetworkConnected(mContext)) {
			request = request.newBuilder()
					.cacheControl(CacheControl.FORCE_CACHE)
					.build();
		}
		Response response = chain.proceed(request);
		Response responseLatest;
		if (NetUtils.isNetworkConnected(mContext)) {
			int maxAge = 0; //有网的情况下 不缓存
			responseLatest = response.newBuilder()
					.removeHeader("Pragma")
					.removeHeader("Cache-Control")
					.header("Cache-Control", "public, max-age=" + maxAge)
					.build();
		} else {
			int maxStale = 60 * 60 * 6; // 没网失效6小时
			responseLatest = response.newBuilder()
					.removeHeader("Pragma")
					.removeHeader("Cache-Control")
					.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
					.build();
		}

		return responseLatest;
	}
}